﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Ict.DataAccess;
using CSF.BusinessCore.BLL;
using CSF.MultiLang.Data;
using CSF.BusinessCore.Data;

namespace CSF.MultiLang.BLL
{
    public class MlTransactionConfigLogic<TEntity> : TransactionConfigLogic<TEntity>
        where TEntity : TransactionConfig, IMlDataConfig, new()
    {
        public bool Insert(TEntity entity, IEnumerable<LangTextContent> names)
        {
            bool result = false;

            using (Executor exec = DataContext.CreateExecutor())
            {
                try
                {
                    exec.BeginTransaction();
                    LangTextDef textDef = new LangTextDef()
                    {
                        PlaceType = LangTextPlaceType.DbField,
                        Place = typeof(TEntity).Name,
                        CreateBy = entity.CreateBy
                    };
                    LangTextDefBLL textDefBll = new LangTextDefBLL();
                    textDefBll.Insert<LangTextDef>(textDef, null, exec);

                    int textid = textDef.Id;

                    LangTextContentBLL textContentBll = new LangTextContentBLL();
                    foreach (LangTextContent name in names)
                    {
                        if (!string.IsNullOrWhiteSpace(name.Content))
                        {
                            name.TextId = textid;
                            name.CreateBy = entity.CreateBy;
                            textContentBll.Insert<LangTextContent>(name, null, exec);
                        }
                    }

                    entity.NameTextId = textid;
                    result = Insert<TEntity>(entity, null, exec);
                    exec.CommitTransaction();
                }
                catch
                {
                    exec.RollbackTransaction();
                    throw;
                }
            }

            return result;
        }

        public bool UpdateById(TEntity entity, IEnumerable<LangTextContent> names, int id)
        {
            bool result = false;

            using (Executor exec = DataContext.CreateExecutor())
            {
                try
                {
                    exec.BeginTransaction();

                    int textid = entity.NameTextId??0;

                    LangTextContentBLL textContentBll = new LangTextContentBLL();
                    foreach (LangTextContent name in names)
                    {
                        if (!string.IsNullOrWhiteSpace(name.Content))
                        {
                            if (name.Id > 0)
                            {
                                name.ModifyBy = entity.ModifyBy;
                                textContentBll.Update<LangTextContent>(name, null, p => p.Id == name.Id, exec);
                            }
                            else
                            {
                                name.TextId = textid;
                                name.CreateBy = entity.ModifyBy;
                                textContentBll.Insert<LangTextContent>(name, null, exec);
                            }
                        }
                        else
                        {
                            textContentBll.DeleteOneById(name.Id);
                        }
                    }

                    entity.NameTextId = textid;
                    int count = Update<TEntity>(entity, null, p => p.Id == id, exec);
                    result = count > 0;
                    exec.CommitTransaction();
                }
                catch
                {
                    exec.RollbackTransaction();
                    throw;
                }
            }

            return result;
        }

    }
}
